﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fanfu.Common;
using fanfu.Business;
using System.Text;
using System.Data;
using System.Configuration;
using fanfu.Model;

public partial class UserControls_Nav : System.Web.UI.UserControl
{
    #region 全局变量
    // 导航Html代码
    public StringBuilder __NavHtml = new StringBuilder();
    // 日志路径
    public string loginLogLink ="../" + ConfigurationManager.AppSettings["LogFilePath"];
    //用户别名
    public string currentUser = CookieHelper.GetCookie("userAlias");
    //新留言
    public string newLeaveword = "0";
    //新评论
    public string newReview = "0";

    #endregion

    /// <summary>
    /// 加载系统菜单
    /// </summary>
    private void LoadSystemMenu()
    {
        string sqlTopMenu = "SELECT * FROM SystemMenu WHERE ParentId=0 AND Flag=1 ORDER BY Ord ASC";
        DataTable topMenu = SqlHelper.GetDataTable(sqlTopMenu);
        foreach (DataRow row in topMenu.Rows)
        {
            __NavHtml.Append("<li>");
            __NavHtml.AppendFormat("<a href=\"javascript:\">{0}</a>", row["MenuName"].ToString());

            string sqlSubMenu = string.Format("SELECT * FROM SystemMenu WHERE ParentId={0} AND Flag=1 ORDER BY Ord ASC", row["ID"].ToString());
            DataTable subMenu = SqlHelper.GetDataTable(sqlSubMenu);
            __NavHtml.Append("<dl>");
            foreach (DataRow sunRow in subMenu.Rows)
            {
                __NavHtml.AppendFormat("<dd><a href=\"{0}\">{1}</a></dd>", sunRow["MenuUrl"].ToString(), sunRow["MenuName"].ToString());
            }
            __NavHtml.Append("</dl>");

            __NavHtml.Append("</li>");
        }
    }

    /// <summary>
    /// 加载新消息数
    /// </summary>
    private void LoadNewMessage()
    {
        string sqlLeaveword = "SELECT COUNT(*) FROM Leaveword WHERE Flag=" + (byte)LeavewordFlag.noVerify;
        newLeaveword = SqlHelper.GetScalar(sqlLeaveword).ToString();
        string sqlReview = "SELECT COUNT(*) FROM Review WHERE Flag=" + (byte)ReviewFlag.noRead;
        newReview = SqlHelper.GetScalar(sqlReview).ToString();
    }

    /// <summary>
    /// 加载页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {   
        //设置用户别名
        if (string.IsNullOrEmpty(currentUser))
            currentUser = "未知";

        //设置日志路径
        if (string.IsNullOrEmpty(loginLogLink))
            loginLogLink = "Log/LoginLog.txt"; //默认路径
        else
            loginLogLink += "LoginLog.txt";    //配置路径

        //加载系统菜单
        LoadSystemMenu();
        //加载新消息数
        LoadNewMessage();
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExit_Click(object sender, EventArgs e)
    {
        new AdminUserRule().UserExit(CookieHelper.GetCookie("userName"));
        string isRememberPwd = CookieHelper.GetCookie("rememberPwd");
        //判断是否记住密码
        if (isRememberPwd != "true")
        {
            CookieHelper.ClearCookie("userName");
        }
        CookieHelper.ClearCookie("loginState");
        CookieHelper.ClearCookie("userAlias");
        Response.Redirect("Login.aspx");
    }
}